/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.drawer.DrawerSelectPopup;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.filters.CommonFilter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

/**
 * 选择滤镜
 *
 * @since 2021-05-17
 */
public class SelectFilterAbilitySlice extends BaseAbilitySlice {
    private Image image1;
    private Text text2;
    private BasePopupView menuPopup;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_abilityslice_select_filter;
    }

    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    private void initView() {
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        text2 = (Text) findComponentById(ResourceTable.Id_text2);
        text2.setText("原图");
        text2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                initDrawerPopup();
            }
        });
        image1.setPixelMap(ResourceTable.Media_test_filters);
    }

    private void initDrawerPopup() {
        DrawerSelectPopup popup = new DrawerSelectPopup(getContext(), text2.getText());
        popup.setItemClickedListener(type -> {
            replaceFraction(type);
        });
        menuPopup = new XPopup.Builder(getContext())
            .enableDrag(true)
            .asCustom(popup)
            .show();
    }

    private void replaceFraction(String menuType) {
        text2.setText(menuType);
        CV4JImage cv4JImage =
            new CV4JImage(PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_filters).get());
        if ("原图".equals(menuType)) {
            image1.setPixelMap(ResourceTable.Media_test_filters);
        } else {
            CommonFilter filter = null;
            try {
                filter = (CommonFilter) Class.forName("com.cv4j.core.filters." + menuType + "Filter").newInstance();
            } catch (IllegalAccessException | ClassNotFoundException | InstantiationException e) {
                e.printStackTrace();
            }
            if (filter != null) {
                image1.setPixelMap(filter.filter(cv4JImage.getProcessor()).get().getImage().toBitmap());
            }
        }
        if (menuPopup != null) {
            menuPopup.dismiss();
        }
    }
}
